<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">

    <title>霁青+翠蓝的Tabbar动画特效</title>
    <!-- 引入字体图标库 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
    <link rel="stylesheet" href="182.css">
</head>

<body>
    <nav>
        <div class="link-bg"></div>
        <ul>
            <li>
                <a class="active">
                    <i class="fa fa-home"></i>
                    <span class="link-text">Home</span>
                </a>
            </li>
            <li>
                <a>
                    <i class="fa fa-inbox"></i>
                    <span class="link-text">Inbox</span>
                </a>
            </li>
            <li>
                <a>
                    <i class="fa fa-user"></i>
                    <span class="link-text">User</span>
                </a>
            </li>
        </ul>
    </nav>
</body>

</html>

<script >
const links=document.querySelectorAll('a'),
      bg=document.querySelector('.link-bg');

// 设置选中态
function setActive(el){
    links.forEach(link=>{
        link.classList.remove('active');
    });
    el.classList.add('active');
}

// 遍历所有a标签，绑定点击事件
links.forEach((link,index)=>{
    link.addEventListener('click',e=>{
        // 阻止事件的默认动作
        e.preventDefault();
        // 计算并设置选中态背景的偏移量
        bg.style.transform='translate('+(190*index)+'px,-50%)';
        // 设置选中态
        setActive(e.currentTarget);
    })
})

</script>